package dk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import dk.beans.Notice;
import dk.beans.subject;
import dk.mapper.NoticeMapper;

@RestController
public class NoticeController {
	@Autowired
	NoticeMapper noticemapper;
@PostMapping("GetNoticeByPage")
public ResponseData GetNoticeByPage(Integer pagenum,Integer count) {
	//设置默认值
	if(pagenum == 0) {
		pagenum = 1;
	}
	if(count == 0) {
		count = 5;
	}
	IPage<Notice> noticePage = new Page<>(pagenum, count);//参数一是当前页，参数二是每页个数
	noticePage = noticemapper.selectPage(noticePage, null);
	ResponseData data = new ResponseData();
	List<Notice> list = noticePage.getRecords();
	QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
	//声明返回用map 第一个字段为total  第二个字段为数据
	Map<String,Object> returnmap = new HashMap();
	Integer total = noticemapper.selectCount(qw.orderByDesc("updatetime"));
	returnmap.put("total", total);
	returnmap.put("data",list);
	data.setData(returnmap);
	data.setMessage("分页查询公告成功");
	data.setCode(200);
	return data;
}
@PostMapping("AddNotice")
public ResponseData AddNotice(String title,String content) {
	ResponseData data = new ResponseData();
	if(StringUtils.isEmpty(title) && StringUtils.isEmpty(content)) {
		data.setCode(201);
		data.setMessage("有字段为空,添加失败");
		return data;
	}
	Notice notice = new Notice();
	notice.setTitle(title);
	notice.setContent(content);
	
	noticemapper.insert(notice);
	data.setCode(200);
	data.setMessage("添加成功");
	return data;
	
}
@PostMapping("SearchNoticeByTitle")
public ResponseData SearchNoticeByTitle(String title,Integer pagenum,Integer count) {
	//设置默认值
		if(pagenum == 0) {
			pagenum = 1;
		}
		if(count == 0) {
			count = 5;
		}
	ResponseData data = new ResponseData();
	QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
	qw.like("title", title);
	IPage<Notice> noticePage = new Page<>(pagenum, count);//参数一是当前页，参数二是每页个数
	
	noticePage = noticemapper.selectPage(noticePage, qw.orderByDesc("updatetime"));
	List<Notice> list = noticePage.getRecords();
	//声明返回用map 第一个字段为total  第二个字段为数据
	Map<String,Object> returnmap = new HashMap();
	Integer total = noticemapper.selectCount(qw);
	returnmap.put("total", total);
	returnmap.put("data",list);
	data.setData(returnmap);
	data.setMessage("分页按条件查询公告成功");
	data.setCode(200);
	return data;
	
}
@PostMapping("GetNoticeByid")
public ResponseData GetNoticeByid(Integer id) {
	ResponseData data = new ResponseData();
	QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
	qw.eq("id", id);
	Notice notice = noticemapper.selectOne(qw);
	data.setData(notice);
	data.setCode(200);
	data.setMessage("根据id获取信息成功");
	return data;
	
}
@PostMapping("EditNoticeByid")
public ResponseData EditNoticeByid(String title,String content,Integer id) {
	ResponseData data = new ResponseData();
	Notice notice = new Notice(id, title, content, null, null);
	QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
	qw.eq("id", id);
	noticemapper.update(notice, qw);
	data.setCode(200);
	data.setMessage("修改公告成功");
    return data;
	
}
@PostMapping("DeleteNoticeByid")
public ResponseData DeleteNoticeByid(Integer id) {
	ResponseData data = new ResponseData();
	QueryWrapper<Notice> qw = new QueryWrapper<Notice>();
	qw.eq("id", id);
	noticemapper.delete(qw);
	data.setCode(200);
	data.setMessage("删除公告成功");
	return data;
}
}
